<?php
  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
        /*<script type="text/JavaScript" src="js/validate.js"></script>
        <script type="text/JavaScript">
        <!--
        function ValidateFields() 
	{
	        return ValidateFieldNotEmpty("newInfoForm", "fname", "first name")
	        && ValidateFieldNotEmpty("newInfoForm", "lname", "last name")
	        && ValidateFieldNotEmpty("newInfoForm", "location", "location");
	}
        //-->
        </script>*/
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Return member info
    	function getMemberInfo()
    	{
			$db = new Connect();

			$sql = "SELECT mid, mName, email, mCity FROM MemberInfo WHERE mid = " . $_SESSION["mid"];

			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
    	}
		
	// Perform the member name change
	// DO NOT ALLOW EMAIL CHANGE SINCE IT IS THE KEY OF TABLE Guest!!!
	function changeMemberInfo() 
	{
		$db = new Connect();
		
		$name = array($_POST['fname'], $_POST['lname']);
		$location = $_POST['location'];
			  
		$sql = "UPDATE MemberInfo " .
			   "SET mName = '" . implode(" ",$name) . "', " .
			   "mCity = '" . $location . "' " .
			   "WHERE mid = " . $_SESSION["mid"];
		
		$result = $db->query($sql);
		
		if(!$result || mysql_affected_rows() == 0) // Failed member change
		{
			return false;
		}
		else // Member changed
		{
			return true;
		}
	}
		
	// Perform the member delete
	function removeMember() 
	{
		$db = new Connect();
		
		$sql = "DELETE FROM MemberInfo, Guest USING MemberInfo INNER JOIN Guest" .
			"ON MemberInfo.email = Guest.email WHERE MemberInfo.mID = " . $_SESSION['mid'];
		
		$result = $db->query($sql);
		
		if(!$result || mysql_affected_rows() == 0) // Failed member delete
		{
			return false;
		}
		else // Member deleted
		{
			//if ($removemember-failed == false)
			print '<meta http-equiv="Refresh" content="0;URL=index.php">';
		}
	}
		
	// Check to see if we received post data and perform appropriate function
	// TODO: Check for empty fields and valid member values
	$changemember_failed = false;
	$successfulmember_failed = false;
	if (isset($_POST["change"]))
	{	
		if(!changeMemberInfo())
			$changemember_failed = !changeMemberInfo();
		else
			$successfulmember_failed = true;
	}
	$removemember_failed = false;
	if (isset($_POST["remove"]))
		$removemember_failed = !removeMember();
		
    ?>
    <div id="main">
    	<p class="title">
    		Settings
    	</p>
    <?php
	if ($changemember_failed == true)
		print "<p class=\"error\">FAILED CHANGING MEMBER INFO!</p>";
	if ($removemember_failed == true)
		print "<p class=\"error\">FAILED REMOVING MEMBER!</p>";
	if ($successfulmember_failed == true)
		print "<p class=\"error\">SUCCESSFULLY CHANGED MEMBER INFO!</p>";

	$rows = getMemberInfo();
	
	if (isset($rows))
	{
		print "<p class=\"normal\">";
		print "Member ID: " . $rows[0]['mid'];
		print "</br>";
		print "Member email: " . $rows[0]['email'];
		print "</br>";
		print "</br>";
		print "<form class=\"normal\" action=\"setting.php\" method=\"post\">";
		$name = explode(' ',$rows[0]['mName']);
	        print "First Name: <input type=\"text\" name=\"fname\" value=\"" . $name[0] . "\">";
		print "</br>";
		print "Last Name: <input type=\"text\" name=\"lname\" value=\"" . $name[1] . "\">";
		print "</br>";
	        print "City: <input type=\"text\" name=\"location\" value=\"" . $rows[0]['mCity'] . "\">";
		print "</br>";
	        print "<input type=\"submit\" class=\"smallButton\" name=\"change\" value=\"Change\"/>";
		print "</br>";
	        print "<input type=\"submit\" class=\"smallButton\" name=\"remove\" value=\"Remove Me\"/>";
	        print "</form>";
		print "</p>";
	}
	else
	{
		print "error";
	}
    ?>
    </div>
</body>
</html>
            
